package com.dex4d.ui.dapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

import com.dex4d.R;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.model.User;
import com.dex4d.ui.dapp.model.DoneEvent;
import com.dex4d.ui.qr.utils.AddressUtils;
import com.wallet.web3.entity.Web3Transaction;

import org.greenrobot.eventbus.EventBus;
import org.web3j.utils.Convert;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;

public class DappDoneActivity extends AppToolBarBaseActivity {

    @BindView(R.id.label_pay_flag)
    TextView label_pay_flag;
    @BindView(R.id.txt_pay_amount)
    TextView txt_pay_amount;
    @BindView(R.id.txt_from)
    TextView txt_from;
    @BindView(R.id.txt_to)
    TextView txt_to;
    @BindView(R.id.txt_fee)
    TextView txt_fee;
    @BindView(R.id.txt_hash)
    TextView txt_hash;

    private Web3Transaction transaction;
    private String hash;

    public static void start(Activity activity, Web3Transaction transaction, String hash) {
        Intent intent = new Intent(activity, DappDoneActivity.class);
        intent.putExtra("TX", transaction);
        intent.putExtra("Hash", hash);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dapp_done;
    }

    @Override
    protected void initVariables() {

        transaction = getIntent().getParcelableExtra("TX");
        hash = getIntent().getStringExtra("Hash");

        showUI(transaction, hash);
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void initToolbar() {
        hideBackView();
    }

    private void showUI(Web3Transaction transaction, String hash) {
        label_pay_flag.setText(getResources().getString(R.string.amount));
        BigDecimal ethAmount = Convert.fromWei(transaction.value.toString(10), Convert.Unit.ETHER);
        String amountString = getEthString(ethAmount.doubleValue()) + " " + "ETH";
        txt_pay_amount.setText(amountString);
        txt_from.setText(AddressUtils.convertAddress(User.getInstance().getAddress()));
        txt_to.setText(AddressUtils.convertAddress(transaction.recipient.toString()));
        txt_fee.setText("");
        txt_hash.setText(AddressUtils.convertAddress(hash));
    }

    public String getEthString(double ethPrice) {
        DecimalFormat df = new DecimalFormat("#.#####");
        df.setRoundingMode(RoundingMode.CEILING);
        String formatted = df.format(ethPrice);
        return formatted;
    }

    @OnClick(R.id.txt_hash)
    public void toWeb2() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_trade_ok, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.manger_trade_done) {
            EventBus.getDefault().post(new DoneEvent(transaction,hash));
            finish();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }
}
